﻿using System;
using System.Collections.Generic;
 
using System.Text;
using GGSharp.Packets;
using System.Net;
using System.IO;

namespace GGSharp.Structures
{
    public class NotifyReply77 : StuctureBase, IStatusReply
    {
        private int _uin;		/* numerek plus flagi w najstarszym bajcie */
        private byte _status;		/* status danej osoby */
        private IPAddress _remoteIP;		/* adres IP bezpośrednich połączeń */
        private short _remotePort;	/* port bezpośrednich połączeń */
        private byte _version;		/* wersja klienta */
        private byte _imageSize;	/* maksymalny rozmiar obrazków w KB */
        private byte _unknown1;		/* 0x00 */
        private int _unknown2;		/* 0x00000000 */
        //private byte _descriptionSize;	/* rozmiar opisu i czasu, nie musi wystąpić */
        private string _description = "";	/* opis, nie musi wystąpić */
        private int _time = 0;		/* czas, nie musi wystąpić */

        protected bool _isAudioEnabled = false;
        protected bool _isAudio7Enabled = false;
        protected bool _isConnectedViaEraOmnix = false;

        public NotifyReply77()
        {
            this.Length = 18;
        }

        public NotifyReply77(BinaryReader reader)
            : this()
        {
            Read(reader);
        }

        #region Properties

        public override int Length
        {
            get
            {
                //jesli jest opis to +1 na dlugosc +1 na \0 +4 na czas
                return (_description.Length > 0 ? 24 + _description.Length : 18);
            }
        }

        public ClientVersion ClientVersion
        {
            get { return TranslateClientVersion(_version); }
        }

        public byte ClientVersionRaw
        {
            get { return _version; }
        }

        public string Description
        {
            get { return _description; }
        }

        public byte ImageSize
        {
            get { return _imageSize; }
        }

        public bool IsConnectedViaEraOmnix
        {
            get { return _isConnectedViaEraOmnix; }
        }

        public bool IsAudioEnabled
        {
            get { return _isAudioEnabled; }
        }

        public bool IsAudio7Enabled
        {
            get { return _isAudio7Enabled; }
        }

        public int Number
        {
            get { return _uin; }
        }

        public IPAddress RemoteIPAddress
        {
            get { return _remoteIP; }
        }

        public short RemotePort
        {
            get { return _remotePort; }
        }

        public GGShowType Show
        {
            get { return TranslateShow(_status); }
        }

        public byte ShowRaw
        {
            get { return _status; }
        }

        #endregion

        

        public override void Read(BinaryReader reader)
        {
            int uin = reader.ReadInt32();
            _isConnectedViaEraOmnix = (uin & UINFLAG_ERA_OMNIX) > 0;
            _isAudioEnabled = (uin & UINFLAG_AUDIO) > 0;
            _isAudio7Enabled = (uin & UINFLAG_AUDIO7) > 0;
            _uin = uin & 0x00ffffff;
            _status = reader.ReadByte();
            _remoteIP = new IPAddress(reader.ReadUInt32());
            _remotePort = reader.ReadInt16();
            _version = reader.ReadByte();
            _imageSize = reader.ReadByte();
            _unknown1 = reader.ReadByte();
            _unknown2 = reader.ReadInt32();

            this.ReadedLength = 18;

            if (HasDescription(Show))
            {
                byte descriptionSize = reader.ReadByte();
                byte[] descriptionBytes = reader.ReadBytes(descriptionSize);

                _description = ReadCP1250Text(descriptionBytes);
                _time = GetTimeFromDescription(descriptionBytes, _description.Length);
                this.ReadedLength += descriptionSize + 1;
            }
        }

        public override void Write(BinaryWriter writer)
        {
            throw new NotImplementedException();
        }
    }
}
